/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs5631threadinglab;

import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author brady
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int rows = -1;
        int cols = -1;
        int numAgents = -1;
        double pickupProb = 1.0;
        double dropProb = 1.0;
        boolean paused = false;
        boolean running = false;
        LinkedList<Agent> agents = new LinkedList<Agent>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("Rows? ");
            rows = Integer.parseInt(br.readLine());

            System.out.print("Columns? ");
            cols = Integer.parseInt(br.readLine());

            System.out.print("Agents? ");
            numAgents = Integer.parseInt(br.readLine());

            System.out.print("Pickup Probability? ");
            pickupProb = Double.parseDouble(br.readLine());

            System.out.print("Drop Probability? ");
            dropProb = Double.parseDouble(br.readLine());

        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        Environment env = new Environment(rows, cols);

        String options =
                "0) Exit\n"
                + "1) Start simulation\n"
                + "2) Pause and display environment\n"
                + "3) Pause and Account for particles\n"
                + "4) Pause all threads\n"
                + "5) Resume all threads\n";
        System.out.println(options);
        while (true) {  
            int opt = -1;

            try {
                opt = Integer.parseInt(br.readLine());
            } catch (Exception e) {
                System.out.print(e.getMessage());
            }

            if (opt == 0) {
                running = false;
                for (Agent a : agents) {
                    a.resumeAgent();
                    a.allowRun = false;
                    try {
                        a.join();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                //may want to inventory particles here
                System.exit(0);

            }
            else if (opt == 1) {
                if (!running) {
                    running = true;
                    for (int i = 0; i < numAgents; i++) {
                        agents.add(new Agent(env, 0, 0, pickupProb, dropProb));
                    }
                    for (Agent a : agents) {
                        a.start();
                    }
                    System.out.println("Starting Simulation...");
                } else {
                    System.out.println("Already running...");
                }
                continue;
            }
            else if (opt == 2) {

                if(!paused){
                System.out.println("Pausing...");
                    for (Agent a : agents) {
                        a.pauseAgent();
                        while(!a.halted){}
                    }
                    paused = true;
                }
                for (int r = 0; r < rows; r++) {
                    for (int c = 0; c < cols; c++) {
                        System.out.print("[" + env.GetCell(r, c).particles.size() + "]");
                    }
                    System.out.println();
                }
                continue;
            }
            else if (opt == 3) {
                if (!paused) {
                    System.out.println("Pausing...");
                    for (Agent a : agents) {
                        a.pauseAgent();
                        while(!a.halted){}
                    }
                    paused = true;
                }
                ArrayList<Integer> ids = new ArrayList<Integer>();
                for (Cell c : env.cells) {
                    for (Particle p : c.particles) {
                        ids.add(p.id);
                    }
                }
                Collections.sort(ids);
                System.out.println("Environment contains " + ids.size() + " particles");
                System.out.println("From " + ids.get(0) + " to " + ids.get(ids.size() - 1));

                int lnl = 0;
                for (int id : ids) {
                    System.out.print(id + " ");
                    lnl++;
                    if (lnl >= 10) {
                        System.out.println();
                        lnl = 0;
                    }
                }
                System.out.println();
                continue;
            }
            else if (opt == 4) {
                System.out.println("Pausing agents...");
                for (Agent a : agents) {
                    a.pauseAgent();
                    while(!a.halted){}
                }
                continue;
            }
            else if (opt == 5) {
                System.out.println("Resuming agents...");
                for (Agent a : agents) {
                    a.resumeAgent();
                }
                continue;
            }
            else {
                System.out.print("Bad option\n");
                continue;
            }
        }
    }
}
